package edu.tyut.game.ui.community

import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import edu.tyut.game.R
import edu.tyut.game.utils.LogUtils

class PostImageViewAdapter(private val context: Context, private val files: MutableList<Uri>): RecyclerView.Adapter<PostImageViewAdapter.ViewHolder>(){

    private val tag = this::class.java.name

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val image: ImageView = view.findViewById(R.id.image_each)
        val removeImage: ImageView = view.findViewById(R.id.remove_pic)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context)
            .inflate(R.layout.layout_post_image_view_item, parent, false)
        return ViewHolder(view)
    }

    // 必须放在这里
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val file = files[position]
        Glide.with(context).load(R.drawable.error).into(holder.removeImage)
        Glide.with(context).load(file).into(holder.image)
        holder.image.setOnClickListener {
            LogUtils.d(tag, "图片点击保存...")
        }
        holder.removeImage.setOnClickListener {
            files.removeAt(position)
            this.notifyItemRemoved(position)
        }
    }

    override fun getItemCount() = files.size
}